using Gtk;

namespace Atlantis
{
    public class ShellView : GLib.Object, IShellView, IInjectRegistry
    {
        private Window window;
        private Registry registry;
        private Notebook notebook1;
        private Notebook notebook2;
        private IWorkspace documentWorkspace;
        private IWorkspace viewsWorkspace;
        private Application presenter;

        public Application Presenter
        {
            get
            {
                return this.presenter;  
            }
            set
            {
                this.presenter = value;
            }
        }

        public Registry Registry
        {
            get
            {
                return this.registry;
            }
            set
            {
                this.registry = value;
            }
        }

        public IWorkspace ViewsWorkspace
        {
            get
            {
                if (this.viewsWorkspace == null)
                {
                    this.viewsWorkspace = new TabWorkspace(this.notebook2);
                    this.registry.Workspaces.set("VIEWS_WORKSPACE", this.viewsWorkspace);
                }
                return this.viewsWorkspace;
            }
        }

        public IWorkspace DocumentWorkspace
        {
            get
            {
                if (this.documentWorkspace == null)
                {
                    this.documentWorkspace = new TabWorkspace(this.notebook1);
                    this.registry.Workspaces.set("DOCUMENT_WORKSPACE", this.documentWorkspace);
                }
                return this.documentWorkspace;
            }
        }

        construct
        {
            Initialize();
        }

        private void Initialize()
        {
            try 
            {
				stderr.printf("Initializing");
                var builder = new Builder();
                builder.add_from_file ("ui.glade");
                builder.connect_signals (this);
                this.window = builder.get_object("ShellView") as Window;
                this.notebook1 = builder.get_object("notebook1") as Notebook;
                this.notebook2 = builder.get_object("notebook2") as Notebook;
                this.window.destroy.connect(Close);
            } 
            catch (Error e) 
            {
                stderr.printf ("Could not load UI: %s\n", e.message);
            } 
        }

        [CCode(instance_pos=-1)]
        public void on_mnuFileNew_activate(MenuItem source)
        {
            ProjectWizardView projectWizardView =  Registry.Get<ProjectWizardView>();    
            projectWizardView.ProjectName = "new project";
            projectWizardView.Show();
        }

        [CCode(instance_pos=-1)]
        public void on_mnuFileExit_activate(MenuItem source)
        {
            Close();
        }

        [CCode(instance_pos=-1)]
        public void on_mnuFileOpen_activate(MenuItem source)
        {
            stderr.printf("on_mnuFileOpen_activate(MenuItem source)\n");
            Registry.Get<IDocumentService>().OpenDocument();
        }
        
        [CCode(instance_pos=-1)]
        public void on_mnuFileSave_activate(MenuItem source)
        {
            stderr.printf("on_mnuFileSave_activate(MenuItem source)\n");
            var document = Registry.Get<IDocumentService>().SelectedDocument;
            var presenter = new DocumentPresenter(this.Registry);
            presenter.Save(document);
        }

        public void Close()
        {
            stderr.printf("Exiting ...");
            Gtk.main_quit();
        }

        public void Show()
        {
            this.window.show_all();
        }

        public void Open()
        {
            
        }
    }
} 
